
// Standard-C Headers
#include <stdio.h>
#include <stdlib.h>

// RTOS Kernel Headers
#include <kernelConfig.h>
#include <task.h>
#include <hwTimer.h>
#include <swTimer.h>
#include <scheduler.h>

// NDS Headers
#include <nds.h>

struct TCB_ARG{
	int arg1;
	int arg2;
	int arg3;
};

struct TCB_ARG arg1;
struct TCB_ARG arg2;
struct TCB_ARG arg3;

// Init Task
struct TCB initTask;
unsigned char initStack[CONFIG_TASK_STACK_SIZE];

int CreateTasks(void *args);

// User Task
struct TCB TestTask_1;
struct TCB TestTask_2;
struct TCB TestTask_3;

unsigned char TestTask_1_Stack[CONFIG_TASK_STACK_SIZE];
unsigned char TestTask_2_Stack[CONFIG_TASK_STACK_SIZE];
unsigned char TestTask_3_Stack[CONFIG_TASK_STACK_SIZE];

extern struct TCB* currentTask;

void Init(void);

int TestTask_1_Func(void *args);
int TestTask_2_Func(void *args);
int TestTask_3_Func(void *args);

void showStack()
{
	int i = 0;

	printf("0x%X  \n", &TestTask_1_Stack[CONFIG_TASK_STACK_SIZE]);

	while(1)
	{
		printf("0x%X  ", TestTask_1_Stack[CONFIG_TASK_STACK_SIZE - i]);
		i++;

		if(i == 32)
			break;
	}

}

int main(void)
{
	Init();

	TaskCreate(&TestTask_1, TestTask_1_Func, (void*)&arg1, CONFIG_MAX_PRIORITY - 1, TestTask_1_Stack, sizeof(TestTask_1_Stack));
	TaskCreate(&TestTask_2, TestTask_2_Func, (void*)&arg2, CONFIG_MAX_PRIORITY - 1, TestTask_2_Stack, sizeof(TestTask_2_Stack));
	TaskCreate(&TestTask_3, TestTask_3_Func, (void*)&arg3, CONFIG_MAX_PRIORITY - 1, TestTask_3_Stack, sizeof(TestTask_3_Stack));

    StartScheduler();

    while(1)
    {
    	printf("IDLE Task\n");
    }

    return 0;
}

void Init(void)
{
	consoleDemoInit();

	InitScheduler();	// Init Lists
	InitTask();			// Init Task (Create IDLE Task)
	InitTick();			// Init H/W Timer - set IRQ interrrupt(TIMER0)
	InitTimer();		// Init S/W Timer (Init Event List)
}


int CreateTasks(void *args)
{
//	TaskCreate(&TestTask_1, TestTask_1_Func, 0, CONFIG_MAX_PRIORITY - 1, TestTask_1_Stack, sizeof(TestTask_1_Stack));
//	TaskCreate(&TestTask_2, TestTask_2_Func, 0, CONFIG_MAX_PRIORITY - 1, TestTask_2_Stack, sizeof(TestTask_2_Stack));
//	TaskCreate(&TestTask_3, TestTask_3_Func, 0, CONFIG_MAX_PRIORITY - 1, TestTask_3_Stack, sizeof(TestTask_3_Stack));

	while(1)
	{
		printf("createTasks\n");
	}
	return 0;
}


int TestTask_1_Func(void *args)
{
	while(1)
	{
		printf("task1\n");
		TaskDelay(1000);
	}

	return 0;
}

int TestTask_2_Func(void *args)
{
	while(1)
	{
		printf("task2\n");
		TaskDelay(500);
	}

	return 0;
}

int TestTask_3_Func(void *args)
{
	while(1)
	{
//		printf("task3\n");
//		TaskDelay(1500);
	}

	return 0;
}

